package com.leapp.sk.view.im

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.AttributeSet
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import cc.vv.lkbasecomponent.util.LKScreenUtil
import com.hyphenate.util.DensityUtil
import com.leapp.sk.R
import java.util.ArrayList

/**
 * 设置选项卡
 * @author  chaohaipeng  on 2018/7/13.
 */
class EaseEmojiconIndicatorView : LinearLayout{
    private var mContext: Context? = null
    private var selectedBitmap: Bitmap? = null
    private var unselectedBitmap: Bitmap? = null

    private var dotViews= mutableListOf<ImageView>()

    private var dotHeight = LKScreenUtil.dp2px(3f)

    constructor(context: Context) : super(context) {
        mContext = context
    }


    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        init(context,attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context

    }

    private fun init(context: Context, attrs: AttributeSet?) {
        mContext = context
        dotHeight = DensityUtil.dip2px(context, dotHeight.toFloat())
        selectedBitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.ease_dot_emojicon_selected)

        unselectedBitmap = BitmapFactory.decodeResource(context.resources, R.mipmap.ease_dot_emojicon_unselected)
        gravity = Gravity.CENTER_HORIZONTAL
    }
    /**
     * 初始化选项卡
     */
    fun init(count: Int) {
        for (i in 0..(count-1)) {
            val rl = RelativeLayout(mContext)
            val params = LinearLayout.LayoutParams(dotHeight, dotHeight)
            val layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT)
            val imageView = ImageView(mContext)
            if (i == 0) {
                imageView.setImageBitmap(selectedBitmap)
                rl.addView(imageView, layoutParams)
            } else {
                imageView.setImageBitmap(unselectedBitmap)
                rl.addView(imageView, layoutParams)
            }
            this.addView(rl, params)
            dotViews?.add(imageView)
        }
    }

    /**
     * 更新选项卡
     */
    fun updateIndicator(count: Int) {
        for (i in dotViews.indices) {
            if (i >= count) {
                dotViews[i].visibility = View.GONE
                (dotViews[i].parent as View).visibility = View.GONE
            } else {
                dotViews[i].visibility = View.VISIBLE
                (dotViews[i].parent as View).visibility = View.VISIBLE
            }
        }
        if (count > dotViews.size) {
            val diff = count - dotViews.size
            for (i in 0..(diff-1)) {
                val rl = RelativeLayout(context)
                val params = LinearLayout.LayoutParams(dotHeight, dotHeight)
                val layoutParams = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)
                layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT)
                val imageView = ImageView(context)
                imageView.setImageBitmap(unselectedBitmap)
                rl.addView(imageView, layoutParams)
                rl.visibility = View.GONE
                imageView.visibility = View.GONE
                this.addView(rl, params)
                dotViews?.add(imageView)
            }
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
            selectedBitmap?.recycle()
            unselectedBitmap?.recycle()

    }

    fun selectTo(position: Int) {
        for (i in dotViews.indices) {
            val imageView = dotViews.get(i)
            imageView.setImageBitmap(unselectedBitmap)
        }
        dotViews[position].setImageBitmap(selectedBitmap)
    }


    fun selectTo(startPosition: Int, targetPostion: Int) {
        val startView = dotViews[startPosition]
        val targetView = dotViews[targetPostion]
        startView.setImageBitmap(unselectedBitmap)
        targetView.setImageBitmap(selectedBitmap)
    }
}